﻿using Microsoft.AspNetCore.Mvc;
using NanjingbeiRailwayStation.Core;
using NanjingbeiRailwayStation.Data.ClickHouse.Models;
using NanjingbeiRailwayStation.Data.ClickHouse.Services;
using NanjingbeiRailwayStation.WebApi.ActionFilters;
using SharpCompress.Common;

namespace NanjingbeiRailwayStation.WebApi.Controllers
{
    /// <summary>
    /// 实时电参数
    /// </summary>

    [Route("[controller]")]
    [TypeFilter(typeof(LoggingActionFilter))]
    public class RealTimeElectricalParametersController : ControllerBase
    {


        /// <summary>
        /// 当前状态
        /// </summary>
        /// <param name="imei"></param>
        /// <returns></returns>
        /// 
        [HttpGet("GetCurrentState")]
        public async Task<ResultRespose<MeterCurrentState>> GetCurrentState(string imei)
        {
            var entity = await new RealTimeElectricalParametersService().GetCurrentState(imei);
            return ResultRespose<MeterCurrentState>.BadWithData(entity);
        }

            /// <summary>
            /// 历史 电量图(按天）
            /// </summary>
            /// <param name="imei"></param>
            /// <param name="dateTimeBegin"></param>
            /// <param name="dateTimeEnd"></param>
            /// <returns></returns>

            [HttpGet("GetHistoricalElectricityChart")]
        public async Task<ResultRespose<List<HistoricalElectricityChart>>> GetHistoricalElectricityChart(string imei, DateTime dateTimeBegin, DateTime dateTimeEnd)
        {
            var newDateTimeBegin = new DateTime(dateTimeBegin.Year, dateTimeBegin.Month, dateTimeBegin.Day, 0, 0, 0);
            var newDateTimeEnd = new DateTime(dateTimeEnd.Year, dateTimeEnd.Month, dateTimeEnd.Day, 23, 59, 59);

            var entities = await new RealTimeElectricalParametersService().GetHistoricalElectricityChart(imei, newDateTimeBegin, newDateTimeEnd);
            return ResultRespose<List<HistoricalElectricityChart>>.BadWithData(entities);

        }

        /// <summary>
        ///  历史 图表走势
        /// </summary>
        /// <param name="imei"></param>
        /// <param name="dateTimeBegin"></param>
        /// <param name="dateTimeEnd"></param>
        /// <returns></returns>

        [HttpGet("GetHistroyChartTrend")]
        public async Task<ResultRespose<MeterChartTrendReponse>> GetHistroyChartTrend(string imei, DateTime dateTimeBegin, DateTime dateTimeEnd)
        {
            var newDateTimeBegin = new DateTime(dateTimeBegin.Year, dateTimeBegin.Month, dateTimeBegin.Day,0,0,0);
            var newDateTimeEnd = new DateTime(dateTimeEnd.Year, dateTimeEnd.Month, dateTimeEnd.Day, 23, 59, 59);

            var entities = await new RealTimeElectricalParametersService().GetMeterHistroyChartTrend(imei, newDateTimeBegin, newDateTimeEnd);


            return ResultRespose<MeterChartTrendReponse>.BadWithData(new MeterChartTrendReponse(entities));

            

        }

        /// <summary>
        /// 图表走势
        /// </summary>
        /// <param name="imei"></param>
        /// <returns></returns>
        [HttpGet("GetChartTrend")]
        public async Task<ResultRespose<MeterChartTrendReponse>> GetChartTrend(string imei)
        {
            var entities = await new RealTimeElectricalParametersService().GetMeterChartTrend(imei);

            return ResultRespose<MeterChartTrendReponse>.BadWithData(new MeterChartTrendReponse(entities));

        }

        /// <summary>
        /// 用电量按日统计(时间范围)
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetDailyElectricityConsumptionStatisticsByDateRange")]
        public async Task<ResultRespose<List<DailyElectricityConsumptionStatistics>>> GetDailyElectricityConsumptionStatisticsByDateRange(DateTime beginDate ,DateTime endDate)
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var entitys = await service.GetDailyElectricityConsumptionStatisticsByDateRange(beginDate, endDate);

            return ResultRespose<List<DailyElectricityConsumptionStatistics>>.OKWithData(entitys);

        }

        /// <summary>
        /// 用电量按日统计
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetDailyElectricityConsumptionStatistics")]
        public async Task<ResultRespose<List< DailyElectricityConsumptionStatistics>>> GetDailyElectricityConsumptionStatistics()
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var entitys=await service.GetDailyElectricityConsumptionStatistics();

            return ResultRespose<List<DailyElectricityConsumptionStatistics>>.OKWithData(entitys);

          

        }


        /// <summary>
        /// 用电量按月统计(时间范围）
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetMonthlyElectricityConsumptionStatisticsByDateRange")]
        public async Task<ResultRespose<List<MonthlyElectricityConsumptionStatistics>>> GetMonthlyElectricityConsumptionStatisticsByDateRange(DateTime beginDate, DateTime endDate)
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            return await service.GetMonthlyElectricityConsumptionStatisticsByDateRange(beginDate,endDate);

        }

        /// <summary>
        /// 用电量按月统计(001,002)
        /// </summary>
        /// <returns></returns>

        [HttpGet("GetMonthlyElectricityConsumptionStatistics")]
        public async Task<ResultRespose<List<MonthlyElectricityConsumptionStatistics>>> GetMonthlyElectricityConsumptionStatistics()
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            return await service.GetMonthlyElectricityConsumptionStatistics();

        }

        /// <summary>
        /// 用电量按年统计 (时间范围)
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAnnualElectricityConsumptionStatisticsByDateRange")]
        public async Task<ResultRespose<List<AnnualStatisticsOfElectricityConsumption>>> GetAnnualElectricityConsumptionStatisticsByDateRange(DateTime beginDate, DateTime endDate)
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            return await service.GetAnnualElectricityConsumptionStatisticsByDateRange(beginDate,endDate);

        }

        /// <summary>
        /// 用电量按年统计 
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAnnualElectricityConsumptionStatistics")]
        public async Task<ResultRespose<List<AnnualStatisticsOfElectricityConsumption>>> GetAnnualElectricityConsumptionStatistics()
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            return await service.GetAnnualElectricityConsumptionStatistics();

        }

        /// <summary>
        /// 塔吊用电监控 的数量 (今日的）
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetTowerCranePowerMonitoringCount")]
        public async Task<ResultRespose<ulong>> GetTowerCranePowerMonitoringCount()
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();
             
            var entitys = await service.GetTowerCranePowerMonitoringCount();
             
            return ResultRespose<ulong>.OKWithData(entitys);

        }


        /// <summary>
        /// 塔吊用电监控 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetTowerCranePowerMonitoringByPage")]
        public async Task<ResultRespose<List<TowerCranePowerMonitoring>>> GetTowerCranePowerMonitoringByPage(int pageIndex,int pageSize)
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var eventReportService = new NanjingbeiRailwayStation.Data.ClickHouse.Services.EventReportService();

            var entitys = await service.GetTowerCranePowerMonitoringByPage(pageIndex,pageSize);

            var alarms = await eventReportService.GetRecentNumberOfAlarmsOfAllDevice();

            var deviceLines = await service.GetDeviceLineCount();
             

            foreach (var entity in entitys)
            {
                var isOnAlarms = alarms.Count(item => item.imei == entity.imei) > 0;
                if (isOnAlarms)
                {
                    entity.TowerCraneStatus = TowerCraneStatus.Alarm;
                }
                else if (deviceLines.Count(item=>item.imei == entity.imei) > 0) 
                {
                    entity.TowerCraneStatus = TowerCraneStatus.Normal;
                }
                else
                {
                    entity.TowerCraneStatus = TowerCraneStatus.Offline;
                }
            }
             
            return ResultRespose<List<TowerCranePowerMonitoring>>.OKWithData(entitys);
        }


        /// <summary>
        /// 塔吊用电监控   TowerCranePowerMonitoring
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetTowerCranePowerMonitoring")]
        public async Task<ResultRespose<List<TowerCranePowerMonitoring>>> GetTowerCranePowerMonitoring()
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var eventReportService = new NanjingbeiRailwayStation.Data.ClickHouse.Services.EventReportService();

            var entitys= await service.GetTowerCranePowerMonitoring();
             
            var alarms= await eventReportService.GetRecentNumberOfAlarmsOfAllDevice();

            var lines = await service.GetDeviceLineCount();

            foreach (var entity in entitys)
            {
               var isOnAlarms= alarms.Count(item=>item.imei==entity.imei) > 0;  
                if (isOnAlarms)
                {
                    entity.TowerCraneStatus = TowerCraneStatus.Alarm;
                } else if (lines.Count(item=>item.imei==entity.imei)>0)
                {
                    entity.TowerCraneStatus = TowerCraneStatus.Normal;
                }
                else
                {
                    entity.TowerCraneStatus = TowerCraneStatus.Offline;
                }
            }
             

            return ResultRespose<List<TowerCranePowerMonitoring>>.OKWithData(entitys);

          
        }

        /// <summary>
        /// 今日用电量(kWh)
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("GetTodayElectricityConsumption")]
        public async Task<ResultRespose<TodayElectricityConsumption>> GetTodayElectricityConsumption()
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var entity= await service.GetTodayElectricityConsumption();

            if (entity != null)
            {
                return ResultRespose<TodayElectricityConsumption>.OKWithData(entity);
            }

            return ResultRespose<TodayElectricityConsumption>.OKWithData(default(TodayElectricityConsumption));

        }


        /// <summary>
        /// 损耗比
        /// </summary>
        /// <returns></returns>

        [HttpGet("GetLossRatio")]
        public async Task<ResultRespose<List<TodayLossRatio>>> GetLossRatio()
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();


            var entitys= await service.GetTodayLossRatio();
             
            return ResultRespose<List<TodayLossRatio>>.OKWithData(entitys);


        }

        /// <summary>
        /// 损耗比 数量
        /// </summary>
        /// <returns></returns>

        [HttpGet("GetLossRatioCount")]
        public async Task<ResultRespose<ulong>> GetLossRatioCount()
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var entitys = await service.GetLossRatioCount();

            return ResultRespose<ulong>.OKWithData(entitys);


        }

        /// <summary>
        /// 损耗比 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetLossRatioByPage")]
        public async Task<ResultRespose<List<TodayLossRatio>>> GetLossRatioByPage(int pageIndex,int pageSize)
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();


            var entitys = await service.GetLossRatioByPage(pageIndex,pageSize);

            return ResultRespose<List<TodayLossRatio>>.OKWithData(entitys);


        }

        /// <summary>
        /// 能耗排名 数量
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetEnergyConsumptionRankingCount")]
        public async Task<ResultRespose<ulong>> GetEnergyConsumptionRankingCount()
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var entitys = await service.GetEnergyConsumptionRankingCount();

            return ResultRespose<ulong>.OKWithData(entitys);

        }

        /// <summary>
        /// 能耗排名 (分页）
        /// </summary>
        /// <returns></returns>

        [HttpGet("GetEnergyConsumptionRankingByPage")]
        public async Task<ResultRespose<List<EnergyConsumptionRanking>>> GetEnergyConsumptionRankingByPage(int pageIndex,int pageSize)
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var entitys = await service.GetEnergyConsumptionRankingByPage(pageIndex,pageSize);

            return ResultRespose<List<EnergyConsumptionRanking>>.OKWithData(entitys);

        }


        /// <summary>
        /// 能耗排名
        /// </summary>
        /// <returns></returns>

        [HttpGet("GetEnergyConsumptionRanking")]
        public async Task<ResultRespose<List<EnergyConsumptionRanking>>> GetEnergyConsumptionRanking(int limit)
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.RealTimeElectricalParametersService();

            var entitys= await service.GetEnergyConsumptionRanking(limit);
           
            return ResultRespose<List<EnergyConsumptionRanking>>.OKWithData(entitys);
         
        }
         
    }
}
